package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.BaseService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("label")
public class BaseController {
    @Autowired
    private BaseService baseService;
    //1.列表数据
    @GetMapping
    public Result findAll(){
       List<Label> labelList= baseService.findAll();
        return new Result(true,StatusCode.OK,"成功",labelList);
    }
    //2. 据id查询数据
    @GetMapping("{labelId}")
    public Result findOne(@PathVariable String labelId){
        Label label = baseService.findOne(labelId);
        return new Result(true,StatusCode.OK,"成功",label);
    }
    //3.添加数据
    @PostMapping
    public Result add(@RequestBody Label label){
        baseService.add(label);
        return new Result(true,StatusCode.OK,"成功");
    }
    //4.修改数据
    @PutMapping("{labelId}")
    public Result update(@RequestBody Label label,@PathVariable String labelId){
        label.setId(labelId);
        baseService.update(label);
        return new Result(true,StatusCode.OK,"成功");
    }
    //5. 删除数据
    @DeleteMapping("{labelId}")
    public Result dele(@PathVariable String labelId){
        baseService.dele(labelId);
        return new Result(true,StatusCode.OK,"成功");
    }
    //6.条件查询
    @PostMapping("search")
    public Result search(@RequestBody Label label){
        baseService.find(label);
        return new Result(true,StatusCode.OK,"成功");
    }
    //7.条件查询带分页
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        //7.1)条件查询带分页
        PageResult pageResult = baseService.search(label,page,size);
        //7.2)返回
        return new Result(true,StatusCode.OK,"条件查询带分页成功！",pageResult);
    }
}
